home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / comm / tcp / ATCP_src_22.lha / AmiTCP-2.2 / src / amitcp / net / sana2copybuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  6.0 KB  |  240 lines

  1. RCS_ID_C="$Id: sana2copybuff.c,v 1.13 1993/11/06 23:39:15 ppessi Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * sana2copybuff.c - Buffer Management Routines for Sana-II Interfaces
  8.  *
  9.  * Last modified: Sun Nov  7 01:37:49 1993 ppessi
  10.  *
  11.  * HISTORY
  12.  * $Log: sana2copybuff.c,v $
  13.  * Revision 1.13  1993/11/06  23:39:15  ppessi
  14.  * Added more information to "mbuf chain short" error message.
  15.  *
  16.  * Revision 1.12  1993/06/04  11:16:15  jraja
  17.  * Fixes for first public release.
  18.  *
  19.  * Revision 1.11  1993/05/16  21:09:43  ppessi
  20.  * RCS version changed.
  21.  *
  22.  * Revision 1.10  1993/05/05  16:10:38  puhuri
  23.  * Fixed cluster allocation code.
  24.  *
  25.  * Revision 1.9  93/04/26  11:53:11  11:53:11  too (Tomi Ollila)
  26.  * Changed include paths of amiga_api.h, amiga_libcallentry.h and amiga_raf.h
  27.  * from kern to api
  28.  * 
  29.  * Revision 1.8  93/04/24  22:46:13  22:46:13  jraja (Jarno Tapio Rajahalme)
  30.  * Removed Define for USECLUSTERS
  31.  * 
  32.  * Revision 1.7  93/04/13  22:22:56  22:22:56  jraja (Jarno Tapio Rajahalme)
  33.  * Changed return from buffer allocation function back to recent form.
  34.  * 
  35.  * Revision 1.6  93/04/12  09:20:40  09:20:40  jraja (Jarno Tapio Rajahalme)
  36.  * Changed reserved mbuf chain so that all mbufs after the header have 
  37.  * clusters.
  38.  * 
  39.  * Revision 1.5  93/04/05  17:46:26  17:46:26  jraja (Jarno Tapio Rajahalme)
  40.  * Changed spl storage variables to spl_t.
  41.  * Changed every .c file to use conf.h.
  42.  * 
  43.  * Revision 1.4  93/03/20  07:11:55  07:11:55  ppessi (Pekka Pessi)
  44.  * Fixed mbuf allocating for headers
  45.  * 
  46.  * Revision 1.3  93/03/05  19:51:16  19:51:16  jraja (Jarno Tapio Rajahalme)
  47.  * Fixed includes (again).
  48.  * 
  49.  * Revision 1.2  93/02/28  22:21:57  22:21:57  ppessi (Pekka Pessi)
  50.  * Made to compile; used RAFn macros.
  51.  * 
  52.  * Revision 1.1  93/02/25  14:34:29  14:34:29  ppessi (Pekka Pessi)
  53.  * Initial revision
  54.  */
  55.  
  56. #include <conf.h>
  57.  
  58. #include <sys/param.h>
  59. #include <sys/systm.h>
  60. #include <sys/malloc.h>
  61. #include <sys/mbuf.h>
  62. #include <sys/socket.h>
  63. #include <sys/socketvar.h>
  64. #include <sys/syslog.h>
  65. #include <sys/synch.h>
  66.  
  67. #include <net/if.h>
  68.  
  69. #if INET
  70. #include <netinet/in.h>
  71. #include <netinet/in_systm.h>
  72. #include <netinet/in_var.h>
  73. #include <netinet/ip.h>
  74. #endif
  75.  
  76. #include <net/if_sana.h>
  77. #include <api/amiga_raf.h>
  78.  
  79. /*
  80.  * allocate mbufs for the size MTU at free_chain for read request
  81.  */
  82. BOOL
  83. ioip_alloc_mbuf(struct IOIPReq *s2rp, ULONG MTU)
  84. {
  85.   register struct mbuf *m, *n;
  86.   register int len = 0;
  87.  
  88.   n = s2rp->ioip_reserved;
  89.  
  90.   /* Check for packet header */
  91.   if (n && (n->m_flags & M_PKTHDR)) {
  92.     /* There is already a full packet  */
  93.     return TRUE;
  94.   }
  95.  
  96.   /* Prepend by a packet header */
  97.   MGETHDR(m, M_NOWAIT, MT_HEADER);
  98.   if (m) { 
  99.     m->m_len = len = MHLEN;
  100.     s2rp->ioip_reserved = m;
  101.     m->m_next = n;
  102.     
  103.     /* Find the end of the free chain */ /* ASSUME THAT THESE HAVE CLUSTERS */
  104.     while (n = m->m_next) {
  105.       len += n->m_len; m = n;
  106.     } 
  107.     
  108.     /*
  109.      * add new (cluster)mbufs to get the desired size
  110.      */
  111.     while (len < MTU) {
  112.       MGET(n, M_NOWAIT, MT_DATA);
  113.       if (n != NULL) {
  114.     MCLGET(n, M_NOWAIT);
  115.     if (n->m_ext.ext_buf != NULL) {
  116.       n->m_len = n->m_ext.ext_size;
  117.       len += n->m_ext.ext_size;
  118.     }
  119.     else {
  120.       m_free(n);
  121.       break;
  122.     }
  123.     m = m->m_next = n;
  124.       }
  125.       else
  126.     break;
  127.     }
  128.     
  129.     s2rp->ioip_reserved->m_pkthdr.len = len;
  130.   }
  131.   if (len < MTU) { 
  132.     m_freem(s2rp->ioip_reserved);
  133.     s2rp->ioip_reserved = NULL;
  134.     return FALSE;
  135.   }
  136.  
  137.   return TRUE;
  138. }
  139.  
  140. /*
  141.  * Copy data from an mbuf chain starting from the beginning,
  142.  * continuing for "n" bytes, into the indicated continuous buffer.
  143.  *
  144.  * NOTE: this WILL be called from INTERRUPTS, so compile with stack checking
  145.  *       disabled and use __saveds if near data is needed.
  146.  */
  147. static SAVEDS BOOL RAF3(m_copy_from_mbuf,
  148.          BYTE*,          to,   a0,
  149.          struct IOIPReq*,from, a1,
  150.          ULONG,          n,    d0)
  151. #if 0
  152. {
  153. #endif
  154.   register struct mbuf *m = from->ioip_packet;
  155.   register unsigned count;
  156.  
  157.   while (n > 0) {
  158. #if DIAGNOSTIC
  159.     if (m == 0) {
  160.       log(LOG_ERR, "m_copy_from_buff: mbuf chain short");
  161.       return FALSE;
  162.     }
  163. #endif
  164.     count = MIN(m->m_len, n);
  165.     bcopy(mtod(m, caddr_t), to, count);
  166.     n -= count;
  167.     to += count;
  168.     m = m->m_next;
  169.   }
  170.   return TRUE;
  171. }
  172.  
  173. /*
  174.  * Copy data from an continuous buffer 'from' to preallocated mbuf chain
  175.  * starting from the beginning, continuing for "n" bytes.
  176.  * Mbufs in the preallocated chain must have their m_len field set to maximum
  177.  * amount of data that they can have.
  178.  * 
  179.  * NOTE: this WILL be called from INTERRUPTS, so compile with stack checking
  180.  *       disabled and use __saveds if near data is needed.
  181.  */
  182. static SAVEDS BOOL RAF3(m_copy_to_mbuf,
  183.          struct IOIPReq*,to,   a0,
  184.          BYTE*,          from, a1,
  185.          ULONG,          n,    d0)
  186. #if 0
  187. {
  188. #endif
  189.   register struct mbuf *f, *m = to->ioip_reserved;
  190.   unsigned totlen = n;
  191.  
  192. #if DIAGNOSTIC
  193.   if (!(m->m_flags & M_PKTHDR)) {
  194.     log(LOG_ERR, "m_copy_to_buff: mbuf chain has no header");
  195.     return FALSE;
  196.   }
  197. #endif
  198.  
  199.   while (n > 0) {
  200. #if DIAGNOSTIC
  201.     if (m == 0) {
  202.       log(LOG_ERR, "m_copy_to_buff: mbuf chain short, "
  203.       "packet len =%ld, reserved=%ld", 
  204.       totlen, to->ioip_reserved->m_pkthdr.len);
  205.       return FALSE;
  206.     }
  207. #endif
  208.     if (n < m->m_len)
  209.       m->m_len = n;
  210.     bcopy(from, mtod(m, caddr_t), m->m_len);
  211.     from += m->m_len;
  212.     n -= m->m_len;
  213.     if (n > 0)
  214.       m = m->m_next;
  215.   }
  216.  
  217.   /*
  218.    * move the packet to the field 'ioip_packet',
  219.    * set total length of the packet and terminate it.
  220.    */
  221.   f = m->m_next;        /* first free mbuf */
  222.   m->m_next = NULL;        /* terminate the chain */
  223.   m->m_flags |= M_EOR;        /* mark end of record */
  224.   to->ioip_packet = to->ioip_reserved;
  225.   to->ioip_packet->m_pkthdr.len = totlen; /* set packet length */
  226.   to->ioip_reserved = f;        /* leftover mbufs */
  227.  
  228.   /*
  229.    * More mbuf flags and interface pointer must be set later
  230.    */
  231.   return TRUE;
  232. }
  233.  
  234. struct TagItem buffermanagement[3] = {
  235.     { S2_CopyToBuff,   (ULONG)m_copy_to_mbuf },
  236.     { S2_CopyFromBuff, (ULONG)m_copy_from_mbuf },
  237.     { TAG_END, }
  238. };
  239.  
  240.